// index.ts
// 获取应用实例
const app = getApp<IAppOption>()

interface Project {
  id: number;
  name: string;
  description: string;
  price: number;
  originalPrice: number;
  image: string;
}

Component({
  data: {
    banners: [
      { id: 1, image: 'http://120.46.146.99:9200/salon/images/banner1.png' },
      { id: 2, image: 'http://120.46.146.99:9200/salon/images/banner2.png' },
      { id: 3, image: 'http://120.46.146.99:9200/salon/images/banner3.png' }
    ],
    recommendedServices: [] as Project[],
    hotProducts: [
      {
        id: 1,
        name: '玻尿酸精华液',
        price: 299,
        image: '/images/product1.jpg'
      },
      {
        id: 2,
        name: '维生素C面膜',
        price: 128,
        image: '/images/product2.jpg'
      },
      {
        id: 3,
        name: '防晒霜SPF50',
        price: 198,
        image: '/images/product3.jpg'
      },
      {
        id: 4,
        name: '洁面乳',
        price: 89,
        image: '/images/product4.jpg'
      }
    ]
  },

  methods: {
    // 跳转到服务页面
    goToServices() {
      wx.switchTab({
        url: '/pages/services/services'
      })
    },

    // 跳转到产品页面
    goToProducts() {
      wx.switchTab({
        url: '/pages/products/products'
      })
    },

    // 跳转到个人中心
    goToProfile() {
      wx.switchTab({
        url: '/pages/profile/profile'
      })
    },

    // 跳转到服务详情
    goToServiceDetail(e: any) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/service-detail/service-detail?id=${id}`
      })
    },

    // 跳转到产品详情
    goToProductDetail(e: any) {
      const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/product-detail/product-detail?id=${id}`
      })
    },

    // 获取推荐服务项目
    getRecommendedServices() {
      
      wx.request({
        url: `${app.globalData.apiConfig.baseUrl}/project/list`,
        method: 'POST',
        data:{
          status: 1
        },
        success: (res: any) => {
          const resp = res.data
          if (resp.code === 200 && res.data) {
            this.setData({
              recommendedServices: resp.data.slice(0, 3)
            })
          } else {
            console.error('获取服务项目失败:', res)
            // 如果接口失败，使用默认数据
            this.setData({
              recommendedServices: [
                {
                  id: 1,
                  name: '深层清洁护理',
                  description: '深层清洁毛孔，去除黑头粉刺，让肌肤更加清透',
                  price: 198,
                  originalPrice: 298,
                  image: '/images/service1.jpg'
                }
              ]
            })
          }
        },
        fail: (err: any) => {
          console.error('请求服务项目接口失败:', err)
          // 网络错误时使用默认数据
          this.setData({
            recommendedServices: [
              {
                id: 1,
                name: '深层清洁护理',
                description: '深层清洁毛孔，去除黑头粉刺，让肌肤更加清透',
                price: 198,
                originalPrice: 298,
                image: '/images/service1.jpg'
              }
            ]
          })
        }
      })
    }
  },

  lifetimes: {
    attached() {
      // 页面加载时获取推荐服务项目
      this.getRecommendedServices()
    }
  }
})
